// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Router from 'vue-router'

import AdminLogin from '@/views/admin/LoginView'
import Main from '@/views/admin/Main'
import Welcome from '@/views/admin/Welcome'
import UserInfo from '@/views/admin/system/UserInfo'
import EmpInfo from '@/views/admin/system/Empinfo'
import Role from '@/views/admin/system/Role'
import Area from '@/views/admin/work/Area'
import Spot from '@/views/admin/work/Spot'
import Order from '@/views/admin/work/Order'
import Log from '@/views/admin/monitor/Log'
import Echarts from '@/views/admin/monitor/Echarts'
import Server from '@/views/admin/monitor/Server'

// 用户端
import Layout from '@/views/user/Layout'
import HomePage from '../views/user/HomePage'
import Userlogin from '@/views/user/UserLogin'
import Cart from '@/views/user/Cart'
import OrderList from '@/views/user/Order'
import User from '@/views/user/User'
import OrderPre from '@/views/user/OrderPre'
import PaySuccess from '@/views/user/PaySuccess'
import Pay from '@/views/user/Pay'
import Goods from '@/views/user/Goods'

import Register from '@/views/admin/Register'
Vue.use(Router)

import { Notify } from 'vant';
//创建一个路由
const routes = [
  { path: '/user/login', name: 'userlogin', component: Userlogin },
  { path: '/admin/login', name: 'adminlogin', component: AdminLogin },
  {
    path: '/main', component: Main,
    children: [
      {path:'/', component: Welcome},
      {path:'/system/userinfo', component: UserInfo },
      { path: '/system/empinfo', component: EmpInfo },
      { path: '/system/role', component: Role },
      { path: '/work/area', component: Area },
      { path: '/work/spot', component: Spot },
      { path: '/work/order', component: Order },
      { path: '/monitor/log', component: Log },
      { path: '/monitor/echarts', component: Echarts },
      { path: '/monitor/server', component:Server },
    ]
  },
  { path: '/', component: Layout,
    children:[
      {path:'/', component: HomePage},
      {path:'/first', component: HomePage},
      { path: 'user/goods', component: Goods },
      { path: 'user/cart', component: Cart },
      { path: '/user/orderpre', component: OrderPre },
      { path: '/user/pay', component: Pay },
      { path: '/paySuccess', component: PaySuccess},
      { path: 'user/orderlies', component: OrderList },
      { path: '/user/personal', component: User },
      
    ]
  },
  {path:'/register',component:Register},

]

//创建一个路由对象，命名为router
const router = new Router({
  routes: routes//使用路由数组初始化路由
})

router.beforeEach((to, from, next) => {
  //alert(to.name+""+to.path);
  if (to.path == '/user/login' || to.path == '/' || to.path=='/register'||to.path=='/first') {
    next();//对登录页和注册页放行
  }
  if (localStorage.getItem("token") == '' || localStorage.getItem("token") == null) {
    //alert(localStorage.getItem("userName"));
    alert('token已失效，请重新登录');
    next('/user/login');//去登录
  } else {
    next();//继续路由
  }

})


//暴露路由对象
export default router
